package com.sxran.im.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.system.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sxran.im.dao.ImUserMapper;
import com.sxran.im.handler.CloseCmdHandler;
import com.sxran.im.handler.LoginCmdHandler;
import com.sxran.im.handler.SearchUserListHandler;
import com.sxran.im.handler.UserInfoCmdHandler;
import com.sxran.im.info.ImFriend;
import com.sxran.im.info.ImGroup;
import com.sxran.im.info.ImUser;
import com.sxran.im.info.ImUserGroup;
import com.sxran.im.sbean.CloseNotificationRequest;
import com.sxran.im.sbean.LoginInputBean;
import com.sxran.im.sbean.LoginOutputBean;
import com.sxran.im.service.JWTService;
import com.sxran.im.service.UserGroupService;
import com.sxran.im.service.UserService;
import io.openvidu.java.client.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户相关的接口：
 * 注册,登陆。
 */
@CrossOrigin(origins = "*")
@RestController
public class UserController {
	@Resource
	private ImUserMapper imUserMapper;
	@Resource
	private JWTService jwtService;
	@Resource
	private UserService userService;

	@Resource
	private UserGroupService userGroupService;


	/**实现离线
	 * @see  CloseCmdHandler
	 * @return
	 */
	@PostMapping("/api/user/offline")
	public void offline(@RequestBody CloseNotificationRequest closeNotificationRequest){
		LoginInputBean loginInputBean = closeNotificationRequest.getLoginInputBean();
		String username = loginInputBean.getUsername();
		ImUser user = new ImUser();
		user.setStatus(1);
		user.setUserId(username);
		imUserMapper.updateById(user);
	}
	/**登录之后ws/tcp连接验证
	 * @see LoginCmdHandler
	 * @return
	 */
	@PostMapping("/api/user/auth")
	public LoginOutputBean auth(@RequestBody LoginInputBean loginInputBean){
		String password = loginInputBean.getPassword();
		String username = loginInputBean.getUsername();

		LambdaQueryWrapper<ImUser> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(ImUser::getUserId, username );
		queryWrapper.eq(ImUser::getPassword, password );
		// check  userInfo
		ImUser imUser = imUserMapper.selectOne(queryWrapper);
		String userId = imUser.getUserId();
		// 设置状态为上线。
		imUser.setStatus(0);
		imUserMapper.updateById(imUser);
		// 群组
		List<ImUserGroup> imUserGroups = userGroupService.selectList(userId);
		List<String> groups = imUserGroups.stream().map( ImUserGroup::getGid ).collect(Collectors.toList());
		// return...
		LoginOutputBean loginOutputBean = new LoginOutputBean();
		loginOutputBean.setUserid(userId);
		loginOutputBean.setToken(imUser.getToken());
		loginOutputBean.setGroupList(groups);
		loginOutputBean.setData(imUser);
		return  loginOutputBean;
	}
	/**
	 * @see SearchUserListHandler
	 * @param user
	 * @return
	 */
	@PostMapping("/api/user/search")
	public CommonResult<List<ImUser>> search(@RequestBody ImUser user){
		LambdaQueryWrapper<ImUser> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.ne(ImUser::getUserId, user.getUserId() );  // 不找自己
		queryWrapper.like(ImUser::getNick,  user.getNick() );   // 搜索昵称
		List<ImUser> imUsers = imUserMapper.selectList(queryWrapper);
		return  CommonResult.success(imUsers);
	}
	/**
	 * @see UserInfoCmdHandler
	 * @param user
	 * @return
	 */
	@PostMapping("/api/user/getUserInfo")
	public CommonResult<ImUser> getUserInfo(@RequestBody ImUser user){
		String userId = user.getUserId();
		// 用户基本信息
		ImUser imUser = userService.selectByUserId(userId);  // imUserMapper.selectById(userId);
		return  CommonResult.success(imUser);
	}
	/**
	 * @return The Session ID
	 */
	@PostMapping("/api/user/register")
	public CommonResult<Integer> register(@RequestBody ImUser user){
//		user.setAvatar("http://images.rednet.cn/articleimage/2013/01/23/1403536948.jpg");
		user.setStatus(2);
//		user.setSign("");
//		user.setTerminal("");
		int insert = imUserMapper.insert(user);
		return CommonResult.success(insert);
	}
	@PostMapping("/api/user/login")
	public CommonResult<ImUser> login(@RequestBody ImUser user){
		LambdaQueryWrapper<ImUser> queryWrapper = new LambdaQueryWrapper<>();
		String userId = user.getUserId();
		queryWrapper.eq(ImUser::getUserId, userId);
		queryWrapper.eq(ImUser::getPassword,user.getPassword());
		ImUser data = imUserMapper.selectOne(queryWrapper);
		if(data ==null){
			return CommonResult.error(10008,"账号/密码不对");
		}else{
			// 模拟生成token内容。
			String token = jwtService.createToken(userId);
			data.setToken(token);
			return CommonResult.success(data);
		}

	}
}
